package mi.payment.utils;

import com.sunmi.pay.hardware.aidlv2.AidlConstantsV2;
import com.sunmi.pay.hardware.aidlv2.security.SecurityOptV2;

import lainey.ui.utils.LogUtil;
import mi.payment.Constant;
import mi.payment.MyApplication;

public class CalculateMacUtil {

    public static byte[] calculateMAC(byte[] data) {
        try {
            SecurityOptV2 securityOptV2 = MyApplication.getInstance().mSecurityOptV2;

            // copy
            byte[] dataIn = new byte[data.length - 13];
            System.arraycopy(data, 13, dataIn, 0, data.length - 13);

            String hexStr = ByteUtil.bytes2HexStr(dataIn);
            LogUtil.e(Constant.TAG, "计算MAC的数据：" + hexStr);

            // sha 1
            dataIn = ShaUtil.sha_1(dataIn);
            hexStr = ByteUtil.bytes2HexStr(dataIn);
            LogUtil.e(Constant.TAG, "SHA_1后的数据：" + hexStr);

            // x9_19
            byte[] dataOut = new byte[8];
            int result = securityOptV2.calcMac(Constant.indexMAK, AidlConstantsV2.Security.MAC_ALG_X9_19, dataIn, dataOut);
            LogUtil.e(Constant.TAG, "calcMac result：" + result);

            hexStr = ByteUtil.bytes2HexStr(dataOut);
            LogUtil.e(Constant.TAG, "MAC加密结果：" + hexStr);

            return dataOut;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new byte[8];
    }


}
